/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.httprequest.behaviour;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.utc.ia04.antsontheweb.utilities.Requete;
import org.utc.ia04.antsontheweb.utilities.RequeteException;

public class HttpRequest extends CyclicBehaviour
{
	//serialVersionUID
	private static final long serialVersionUID = 8643011156546254514L;

	//Error
	public static final String REQUEST_FAILED = "Probleme lors de la requete";
	
	private HttpClient httpClient;

	public HttpRequest()
	{
		super();
		httpClient = new DefaultHttpClient();
		
		/* ---------------- b) Run at UTC
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("proxyweb.utc.fr", 3128, "http"));
		// -------------------------------*/
	}
	
	public void action()
	{
		ACLMessage m = myAgent.receive();
		if(m!=null)
		{
			try
			{
				Requete query = Requete.deserialize_withException(m.getContent(), myAgent, m);
				switch(query.getType())
				{
					case Requete.ANTPATHFINDER_ASKHTMLCONTENT:
						String url = query.getContent();
						ACLMessage reply = m.createReply();
						String html;
						Requete req = new Requete();
						req.setType(Requete.HTTPREQ_TO_PARSER);
						try {
							html = doRetrieve(url);
							reply.setPerformative(ACLMessage.INFORM);	
							req.setContent(html);
							reply.setContent(req.serialize());
						} catch (Exception e) {
							reply.setPerformative(ACLMessage.FAILURE);
							req.setContent(HttpRequest.REQUEST_FAILED);
							reply.setContent(req.serialize());
						}
						
						myAgent.send(reply);
						break;
				}
			}
			catch(RequeteException e) { }

		}else block();
	}

	private String doRetrieve(String url) throws ClientProtocolException, IOException
	{
		HttpGet httpget = new HttpGet(url);
		HttpResponse response = httpClient.execute(httpget);
		HttpEntity resEntity = response.getEntity();
		return EntityUtils.toString(resEntity);
	}
}
